package model;

import java.io.Serializable;

public class Message implements Serializable {

    private User messageUser;
    private String messageDate;
    private String messageContent;

    public Message() {
    }

    public Message(User user, String date, String content) {
        messageUser = user;
        messageDate = date;
        messageContent = content;
    }

    public User getMessageUser() {
        return this.messageUser;
    }

    public void setMessageUser(User user) {
        this.messageUser = user;
    }

    public String getMessageDate() {
        return this.messageDate;
    }

    public void setMessageDate(String date) {
        this.messageDate = date;
    }

    public String getMessageContent() {
        return this.messageContent;
    }

    public void setMessageContent(String content) {
        this.messageContent = content;
    }

    @Override
    public String toString() {
        return "Message{" + "messageUser=" + messageUser.getUserNickName()
                + ", messageDate=" + messageDate + ", messageContent=" + messageContent + '}';
    }

    public static Message stringToMessage(String content) {
        Message m = new Message();
        char replace1 = '{';
        char replace2 = '}';
        char empty = ' ';
        content = content.replace(replace1, empty);
        content = content.replace(replace2, empty);
        String[] firstSplit = content.split(",");
        for (int i = 0; i < firstSplit.length; i++) {
            String[] secondSplit = firstSplit[i].split("=");
            if (i == 0) {
                User u = new User();
                u.setUserNickName(secondSplit[1]);
                m.setMessageUser(u);
            } else if (i == 1) {
                m.setMessageDate(secondSplit[1]);
            } else if (i == 2) {
                m.setMessageContent(secondSplit[1]);
            }
        }
        return m;
    }
}
